其他
ggplot 随心所欲的添加注释
黑夜洒满了星星
1引言
本文思路来自于 小明的数据分析笔记本 推文跟着Nature Plants学作图:R语言ggplot2画分组折线图并对坐标轴添加一些额外注释
。在很多文章里也会看见各种复杂的图形。
本文借鉴以上思路绘制几个图形分享给大家,可以借鉴参考。
2连续变量
library("ggplot2")
library("grid")
library("ggsci")
# load data
data("mtcars")
# plot
ggplot(mtcars,aes(x = mpg,y = disp)) +
geom_line(size = 1,aes(color = mpg)) +
scale_color_gradient2(low = 'red',mid = '#CCFF99',high = '#99CCFF',
name = '',midpoint = 22.5) +
# botomn rect
annotation_custom(grob = rectGrob(gp = gpar(col = NA,
fill = '#99CCCC')),
xmin = 10,xmax = 15,
ymin = -25,ymax = 0) +
annotation_custom(grob = rectGrob(gp = gpar(col = NA,
fill = '#FFCC99')),
xmin = 15,xmax = 25,
ymin = -25,ymax = 0) +
annotation_custom(grob = rectGrob(gp = gpar(col = NA,
fill = '#FF6666')),
xmin = 25,xmax = 35,
ymin = -25,ymax = 0) +
# point
annotation_custom(grob = circleGrob(gp = gpar(fill = '#33CC33',col = 'white',
lty = 'solid',lwd = 1.5)),
xmin = 11.5,xmax = 13.5,
ymin = -25,ymax = 0) +
annotation_custom(grob = circleGrob(gp = gpar(fill = '#99CCFF',col = 'white',
lty = 'solid',lwd = 1.5)),
xmin = 19,xmax = 21,
ymin = -25,ymax = 0) +
annotation_custom(grob = circleGrob(gp = gpar(fill = '#FFFF99',col = 'white',
lty = 'solid',lwd = 1.5)),
xmin = 29,xmax = 31,
ymin = -25,ymax = 0) +
# right
annotation_custom(grob = segmentsGrob(gp = gpar(col = "#CC0033",
lwd = 2,
lineend = "square"),
arrow = arrow(angle=20,
length = unit(2,'mm'))),
xmin = 36,xmax = 36,
ymin = 260,ymax = max(mtcars$disp))+
annotation_custom(grob = segmentsGrob(gp = gpar(col = "grey50",
lwd = 2,
lineend = "square"),
arrow = arrow(angle=20,ends = 'first',
length = unit(2,'mm'))),
xmin = 36,xmax = 36,
ymin = min(mtcars$disp),ymax = 240) +
# left
annotation_custom(grob = rectGrob(gp = gpar(col = 'black',alpha = 0.7,
fill = '#0099CC')),
xmin = 5.5,xmax = 6.5,
ymin = min(mtcars$disp),ymax = 250) +
annotation_custom(grob = rectGrob(gp = gpar(col = 'black',alpha = 0.7,
fill = '#009933')),
xmin = 5.5,xmax = 6.5,
ymin = 250,ymax = max(mtcars$disp)) +
theme_bw(base_size = 16) +
coord_cartesian(clip = "off") +
theme(aspect.ratio = 0.6,
plot.margin = margin(0,0,0,1,unit = 'cm'),
legend.position = 'right') +
xlab('') + ylab('') +
guides(color = guide_colorbar(barheight = 14))
3分类变量
# test data
col_df <- data.frame(gene = c('MYC','SOX2','NANOG','ACTB','GAPDH'),
exp = rev(seq(0.6,1,0.1)))
col_df
# gene exp
# 1 MYC 1.0
# 2 SOX2 0.9
# 3 NANOG 0.8
# 4 ACTB 0.7
# 5 GAPDH 0.6
pal_lancet()(8)
# [1] "#00468BFF" "#ED0000FF" "#42B540FF" "#0099B4FF" "#925E9FFF" "#FDAF91FF" "#AD002AFF"
# [8] "#ADB6B6FF"
# order
col_df$gene <- factor(col_df$gene,levels = col_df$gene)
# plot
ggplot(col_df,aes(x = gene,y = exp,fill = gene)) +
geom_col(show.legend = F,fill = '#FF9900',width = 0.6) +
# botomn rect
annotation_custom(grob = rectGrob(gp = gpar(col = NA,
fill = '#00468BFF')),
xmin = 0.7,xmax = 1.3,
ymin = -0.15,ymax = -0.1) +
annotation_custom(grob = rectGrob(gp = gpar(col = NA,
fill = '#ED0000FF')),
xmin = 1.7,xmax = 2.3,
ymin = -0.15,ymax = -0.1) +
annotation_custom(grob = rectGrob(gp = gpar(col = NA,
fill = '#42B540FF')),
xmin = 2.7,xmax = 3.3,
ymin = -0.15,ymax = -0.1) +
annotation_custom(grob = rectGrob(gp = gpar(col = NA,
fill = '#0099B4FF')),
xmin = 3.7,xmax = 4.3,
ymin = -0.15,ymax = -0.1) +
annotation_custom(grob = rectGrob(gp = gpar(col = NA,
fill = '#925E9FFF')),
xmin = 4.7,xmax = 5.3,
ymin = -0.15,ymax = -0.1) +
scale_fill_lancet(name = '') +
scale_y_continuous(expand = c(0,0)) +
theme_classic(base_size = 16) +
xlab('') +
theme(aspect.ratio = 1,
axis.text.x = element_text(color = 'black')) +
coord_cartesian(clip = "off")
4结尾
确定好坐标就可以添加自己想要绘制的图形了。
欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群
(微信交流群需收取20元入群费用(防止骗子和便于管理)
)。
群二维码:
老俊俊微信:
知识星球:
所以今天你学习了吗?
今天的分享就到这里了,敬请期待下一篇!
最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!
如果觉得对您帮助很大,赏杯快乐水喝喝吧!
往期回顾
◀...